package com.hshh.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hshh.common.Constant;
import com.hshh.domain.UserInfoSession;
@SuppressWarnings("unchecked")
public class CommonCommand {
	/**
	 * request => Map 변환
	 * @param request
	 * @return
	 */
	public Map<String, Object> requestToMap(HttpServletRequest request){
		Enumeration<String> keys = request.getParameterNames();
		Map<String, Object> result = new HashMap<String, Object>();
		while(keys.hasMoreElements()){
			String key = keys.nextElement();
			result.put(key, request.getParameter(key));
		}
		return result;
	}
	
	/**
	 * 사용자 접속 세션 확인
	 * @param request
	 * @return 사용자 접속 세션
	 */
	public UserInfoSession getSession(HttpServletRequest request){
		UserInfoSession userInfoSession = (UserInfoSession) request.getSession().getAttribute(Constant.SESSION_ID);
		return userInfoSession;
	}
	/**
	 * 세션 여부 체크
	 * @param request
	 * @return 접속중 - true
	 */
	public boolean getSessionAuth(HttpServletRequest request){
		if(request.getSession().getAttribute(Constant.SESSION_ID) == null){
			return false;
		}
		else{
			return true;
		}
	}
	/**
	 * 접속자 권한 확인
	 * @param request
	 * @return 권한
	 */
	public String getPermission(HttpServletRequest request){
		if(this.getSessionAuth(request)){
			return this.getSession(request).getUser_role();
		}
		else{
			return null;
		}
	}
}
